<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地址" prop="regionName">
        <el-cascader
          placeholder="请选择地址"
          v-model="areaSelects"
          :options="areaOptions"
          :props="areaProps"
          filterable
        ></el-cascader>
      </el-form-item>
<!--      <el-form-item label="省" prop="provinceName">-->
<!--        <el-input-->
<!--          v-model="queryParams.provinceName"-->
<!--          placeholder="请输入省名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="市" prop="cityName">-->
<!--        <el-input-->
<!--          v-model="queryParams.cityName"-->
<!--          placeholder="请输入市名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区/县" prop="countyName">-->
<!--        <el-input-->
<!--          v-model="queryParams.countyName"-->
<!--          placeholder="请输入区/县名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="省编码" prop="province" v-show="false">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市编码" prop="city" v-show="false">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县编码" prop="county" v-show="false">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入区/县编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="行政区域编码" prop="regionCode" v-show="false">-->
<!--        <el-input-->
<!--          v-model="queryParams.regionCode"-->
<!--          placeholder="请输入行政区域编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="行政区域" prop="regionName">-->
<!--        <el-input-->
<!--          v-model="queryParams.regionName"-->
<!--          placeholder="请输入行政区域名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:goodsCenters:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:goodsCenters:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:goodsCenters:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['shop:goodsCenters:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsCentersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="商品自提点id" align="center" prop="id" />-->
      <el-table-column label="省名称" align="center" prop="provinceName" />
      <el-table-column label="市名称" align="center" prop="cityName" />
      <el-table-column label="区/县名称" align="center" prop="countyName" />
      <el-table-column label="自提点电话" align="center" prop="centersPhone" />
<!--      <el-table-column label="行政区域名称" align="center" prop="regionName" />-->
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:goodsCenters:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:goodsCenters:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品自提点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="22">
          <el-form-item label="地址选择" prop="regionCode">
            <el-cascader
              placeholder="请选择地址"
              v-model="areaEditSelects"
              :options="areaOptions"
              :props="areaProps"
              filterable
              style="width: 100%;"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="省" prop="provinceName">
            <el-input v-model="form.provinceName" placeholder="请在“地址选择”中下拉选择" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="市" prop="provinceName">
            <el-input v-model="form.cityName" placeholder="请在“地址选择”中下拉选择" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="区/县" prop="provinceName">
            <el-input v-model="form.countyName" placeholder="请在“地址选择”中下拉选择" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="联系电话" prop="centersPhone">
            <el-input v-model="form.centersPhone" placeholder="请输入自提点电话" :maxlength="18"/>
          </el-form-item>
        </el-col>
        <el-col :span="22">
<!--        <el-form-item label="省编码" prop="province">-->
<!--          <el-input v-model="form.province" placeholder="请输入省编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="市编码" prop="city">-->
<!--          <el-input v-model="form.city" placeholder="请输入市编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区/县编码" prop="county">-->
<!--          <el-input v-model="form.county" placeholder="请输入区/县编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="行政区域编码" prop="regionCode">-->
<!--          <el-input v-model="form.regionCode" placeholder="请输入行政区域编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="行政区域名称" prop="regionName">-->
<!--          <el-input v-model="form.regionName" placeholder="请输入行政区域名称" />-->
<!--        </el-form-item>-->
        </el-col>
        <el-col :span="22">
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoodsCenters, getGoodsCenters, delGoodsCenters, addGoodsCenters, updateGoodsCenters, handleRegionCascader } from "@/api/shop/goodsCenters";
import { listArea } from "@/api/baseplatform/area";

export default {
  name: "GoodsCenters",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品自提点表格数据
      goodsCentersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provinceName: null,
        cityName: null,
        countyName: null,
        province: null,
        city: null,
        county: null,
        regionCode: null,
        regionName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        provinceName: [
          { required: true, trigger: "blur", message: "请进行地址选择" }
        ],
        address: [
          { required: true, trigger: "blur", message: "请输入详细地址" }
        ],
        centersPhone: [
          { required: true, trigger: "blur", message: "请输入自提点联系电话" }
        ],
      },
      // 区域地址选中项
      areaSelects: [],
      // 区域地址编辑框选中项
      areaEditSelects: [],
      // 区域地址数据源
      areaOptions: [],
      //
      areaProps: {
        // checkStrictly: true,
        lazy: true,
        lazyLoad (node, resolve) {
          let nodes = [];
          if (node.level > 0) {
            const params = {pAreaCode: node.value}
            listArea(params).then(res => {
              nodes = handleRegionCascader(res.data, 'areaCode', 'areaName', 'pAreaCode');
              resolve(nodes);
            });
          }
        }
      },
      // 查询参数
      queryAreaParams: {
        areaCode: null,
        areaName: null,
        pAreaCode: null,
        areaLevel: null
      },
      filterForm: {
        city: [],
        district: [],
        province:[],
      }
    };
  },
  created() {
    this.getList();
    this.getAreaList({areaLevel: '1'});
  },
  methods: {
    /** 查询商品自提点列表 */
    getList() {
      this.loading = true;
      listGoodsCenters(this.queryParams).then(response => {
        this.goodsCentersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAreaList(params) {
      listArea(params).then(res => {
        this.areaOptions = handleRegionCascader(res.data, 'areaCode', 'areaName', 'pAreaCode');
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        provinceName: null,
        cityName: null,
        countyName: null,
        province: null,
        city: null,
        county: null,
        regionCode: null,
        regionName: null,
        address: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.areaSelects = [];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.regionName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品自提点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoodsCenters(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品自提点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsCenters(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsCenters(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.regionName || this.names;
      this.$modal.confirm('是否确认删除【' + names + '】的商品自提点？').then(function() {
        return delGoodsCenters(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/goodsCenters/export', {
        ...this.queryParams
      }, `goodsCenters_${new Date().getTime()}.xlsx`)
    }
  },
  watch: {
    areaEditSelects(val) {
      const data = JSON.parse(localStorage.getItem("REGION_MAP"));
      this.form.regionCode = val[val.length - 1];
      this.form.regionName = data[val[val.length - 1]];
      if (val.length === 3) {
        this.form.province = val[0];
        this.form.provinceName = data[val[0]];
        this.form.city = val[1];
        this.form.cityName = data[val[1]];
        this.form.county = val[2];
        this.form.countyName = data[val[2]];
      } else if (val.length === 2) {
        this.form.province = val[0];
        this.form.provinceName = data[val[0]];
        this.form.city = val[1];
        this.form.cityName = data[val[1]];
      } else if (val.length === 1) {
        this.form.province = val[0];
        this.form.provinceName = data[val[0]];
      }
    },
    areaSelects(val) {
      this.form.regionCode = val[val.length - 1];
      if (val.length === 3) {
        this.queryParams.province = val[0];
        this.queryParams.city = val[1];
        this.queryParams.county = val[2];
      } else if (val.length === 2) {
        this.queryParams.province = val[0];
        this.queryParams.city = val[1];
      } else if (val.length === 1) {
        this.queryParams.province = val[0];
      }
    }
  }
};
</script>
